﻿Imports TNCDAL
Imports System.Data.Linq
Imports System.Linq

Public Class CUnit
    Dim db As New TanNamChinhDataContext

    Public Function SelectAllRecord() As IQueryable(Of tblUnit)
        Dim unit = From p In db.tblUnits
                   Where p.IsActive = True
                  Select p Order By p.UnitName

        Return unit
    End Function

    Public Function SelectAllRecordByName(ByVal Name As String) As IQueryable(Of tblUnit)
        Dim unit = From p In db.tblUnits
                   Where p.IsActive = True And p.UnitName.ToLower.Contains(Name.ToLower)
                  Select p Order By p.UnitName

        Return unit
    End Function

    Public Function SelectRecordById(ByVal id As Integer) As tblUnit
        Dim unit = (From p In db.tblUnits
                 Where p.ID_Unit = id
                 Select p).Single
        Return unit
    End Function

    Public Sub Insert(ByVal obj As tblUnit)
        db.tblUnits.InsertOnSubmit(obj)
        db.SubmitChanges()
    End Sub

    Public Sub Update(ByVal obj As tblUnit)
        Dim unit = SelectRecordById(obj.ID_Unit)
        unit.UnitName = obj.UnitName
        db.SubmitChanges()
    End Sub

    Public Sub Delete(ByVal id As Integer)
        Dim unit = SelectRecordById(id)
        unit.IsActive = False
        db.SubmitChanges()
        'Dim unit = SelectRecordById(id)
        'db.tblUnits.DeleteOnSubmit(unit)
        'db.SubmitChanges()
    End Sub
End Class
